package com.school.manager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.school.manager.common.Constants;
import com.school.manager.domaim.SysClass;
import com.school.manager.domaim.TecClassStu;
import com.school.manager.entity.ResponseEntity;
import com.school.manager.entity.po.SysClassPo;
import com.school.manager.entity.po.TecClassStuPo;
import com.school.manager.entity.vo.SysClassVo;
import com.school.manager.enums.ResponseEnum;
import com.school.manager.service.ISysClassService;
import com.school.manager.service.ITecClassStuService;
import com.school.manager.utils.ConvertUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * description：班级控制器
 * date：2022/10/133:18 下午
 */

@RestController
@Api(tags = "班级")
@RequestMapping(Constants.PROGRAM_PATH + "class")
public class SysClassController {
	
	@Autowired
    private ISysClassService tecClassService;
	
	@Autowired
    private ITecClassStuService tecClassStuService;

	@PostMapping("add")
    @ApiOperation("新增班级")
    @ApiImplicitParam(name = "班级", value = "tecClassPo", dataTypeClass = SysClassPo.class)
    public ResponseEntity add(@RequestBody SysClassPo tecClassPo) {
        return ResponseEnum.SUCCESS.success(tecClassService.save(ConvertUtil.convertObj(tecClassPo, SysClass.class)));
    }


    @PostMapping("edit")
    @ApiOperation("修改班级")
    @ApiImplicitParam(name = "班级", value = "tecClassPo", dataTypeClass = SysClassPo.class)
    public ResponseEntity edit(@RequestBody SysClassPo tecClassPo) {
        return ResponseEnum.SUCCESS.success(tecClassService.updateById(ConvertUtil.convertObj(tecClassPo, SysClass.class)));
    }


    @GetMapping("get/{id}")
    @ApiOperation("班级详情")
    @ApiImplicitParam(name = "班级主键", value = "id", dataTypeClass = String.class)
    public ResponseEntity get(@PathVariable("id") String id) {
        SysClass tecClass = tecClassService.getById(id);
        return ResponseEnum.SUCCESS.success(ConvertUtil.convertObj(tecClass, SysClassVo.class));
    }


    @PostMapping("page")
    @ApiOperation("班级分页列表")
    @ApiImplicitParam(name = "班级Po", value = "tecClassPo", dataTypeClass = SysClassPo.class)
    public ResponseEntity page(@RequestBody SysClassPo tecClassPo) {
        return ResponseEnum.SUCCESS.success(tecClassService.page(tecClassPo));
    }
    
    @PostMapping("remove/user")
    @ApiOperation("移除班级人员")
    @ApiImplicitParam(name = "班级Po", value = "tecClassPo", dataTypeClass = SysClassPo.class)
    public ResponseEntity removeUser(@RequestBody TecClassStuPo tecClassStuPo) {
    	tecClassStuService.remove(new LambdaQueryWrapper<TecClassStu>()
    			.eq(TecClassStu::getUserId, tecClassStuPo.getUserId())
    			.eq(TecClassStu::getClassId, tecClassStuPo.getClassId()));
    	SysClass sysClass = tecClassService.getById(tecClassStuPo.getClassId());
    	sysClass.setQuantity(sysClass.getQuantity() - 1);
    	tecClassService.updateById(sysClass);
        return ResponseEnum.SUCCESS.success();
    }
    
    @PostMapping("add/user")
    @ApiOperation("添加班级人员")
    @ApiImplicitParam(name = "班级Po", value = "tecClassPo", dataTypeClass = SysClassPo.class)
    public ResponseEntity addUser(@RequestBody TecClassStuPo tecClassStuPo) {
    	tecClassStuService.save(ConvertUtil.convertObj(tecClassStuPo, TecClassStu.class));
    	SysClass sysClass = tecClassService.getById(tecClassStuPo.getClassId());
    	sysClass.setQuantity(sysClass.getQuantity() + 1);
    	tecClassService.updateById(sysClass);
        return ResponseEnum.SUCCESS.success();
    }
    
}
